<template>
  <div class="trouble" :class="{ active: flag }">
    <div class="point">隐患</div>
    <div class="info">
      <div class="title">隐患信息</div>
      <ul class="intro">
        <li>
          <span>隐患点</span>
          <p>{{ info.b0102 }}</p>
        </li>
        <li>
          <span>上报来源</span>
          <p>{{ info.e0207Name }}</p>
        </li>
        <li>
          <span>隐患类型</span>
          <p>{{ info.e0204Name }}</p>
        </li>
        <li>
          <span>隐患等级</span>
          <p>{{ info.e0206Name }}</p>
        </li>
      </ul>
      <div class="sub-title">
        <p>隐患描述</p>
        <span v-if="info.e0220 == '1'">已更新</span>
      </div>
      <div class="sub-desc">{{ info.e0208 }}</div>
      <ul class="photograph">
        <li v-for="(item, index) in photoList || []" :key="index">
          <img :src="item.src" @click="checkView(item)" />
          <p @click="exportFile(item)">文件下载</p>
        </li>
      </ul>
    </div>
    <a-modal
      title="视频"
      v-model="videoShow"
      centered
      width="880px"
      destroyOnClose
      :footer="null"
    >
      <div class="ry-video-wrap">
        <video ref="videoPlayer" class="video-js">
          <p class="vjs-no-js">不支持播放</p>
        </video>
      </div>
    </a-modal>
  </div>
</template>

<script>
import fileType from "@/mixins/fileType.js";
export default {
  mixins: [fileType],
  props: {
    info: {
      type: Object,
      default: () => {},
    },
    flag: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      videoShow: false,
    };
  },
  computed: {
    photoList() {
      return (this.info.materialList || []).map((el) => {
        return {
          ...this.getFileType(el.showId, el.fileSuffix),
          ...el,
        };
      });
    },
  },
  created() {},
  methods: {
    // 查看图片或视频
    checkView(item) {
      if (item.type === "image") {
        this.$viewerApi({
          images: [item.src],
        });
      } else if (item.type === "video") {
        this.videoShow = true;
        this.$nextTick(() => {
          this.createVideo(item.showId);
        });
      }
    },
    createVideo(id) {
      let src =
        process.env.VUE_APP_FILE_BASE_URL + `/api/common/file/show/whdn/` + id;
      let options = {
        autoplay: false,
        controls: true,
        sources: [
          {
            src,
            type: "video/mp4",
          },
        ],
      };
      this.$video(this.$refs.videoPlayer, options);
    },
    // 文件下载
    exportFile(item) {
      let a = document.createElement("a"); // 创建a标签
      a.setAttribute("download", true); // download属性
      a.setAttribute("target", "_blank"); // download属性
      a.setAttribute(
        "href",
        `${process.env.VUE_APP_FILE_BASE_URL}/api/common/file/show/whdn/${item.showId}`
      ); // href链接
      a.click(); // 自执行点击事件
      document.body.removeChild(a);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.trouble {
  position: relative;
  display: flex;
  align-items: flex-start;
  padding-bottom: 20px;
  .point {
    flex: 0 0 auto;
    width: 52px;
    line-height: 20px;
    margin-right: 15px;
    padding: 4px 8px;
    background: #156abc;
    border-radius: 3px;
    color: #fff;
    text-align: center;
  }
  .info {
    flex: 1;
    padding: 5px 20px 15px;
    border: 1px solid #bebebe;
    background: #e2e5ef;
    .title {
      line-height: 34px;
      border-bottom: 1px solid #c3c3c3;
      font-weight: bold;
      color: #176bbc;
    }
    .intro {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      li {
        flex: 0 0 auto;
        min-width: 23%;
        height: 36px;
        margin-top: 15px;
        margin-right: 2.66%;
        display: flex;
        align-items: center;
        span {
          flex: 0 0 auto;
          width: 65px;
        }
        p {
          flex: 1;
          height: 36px;
          line-height: 34px;
          border: 1px solid #3e9fc8;
          padding: 0 8px;
          border-radius: 4px;
        }
        &:nth-child(4) {
          margin-right: 0;
        }
      }
    }
    .sub-title {
      display: flex;
      align-items: center;
      padding: 10px 0;
      p {
        flex: 0 0 auto;
        margin-right: 10px;
      }
      span {
        font-size: 12px;
        color: #fa5454;
      }
    }
    .sub-desc {
      min-height: 56px;
      line-height: 24px;
      border: 1px solid #3e9fc8;
      padding: 3px 8px;
      border-radius: 4px;
    }
    .photograph {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      li {
        flex: 0 0 auto;
        width: 120px;
        margin-right: 15px;
        margin-top: 15px;
        img {
          width: 120px;
          height: 90px;
          margin-right: 15px;
          margin-top: 15px;
          border-radius: 8px;
          cursor: pointer;
        }
        p {
          width: 120px;
          margin-top: 10px;
          color: #1492ff;
          text-align: center;
          cursor: pointer;
        }
      }
    }
  }
  &.active {
    &:after {
      content: "";
      position: absolute;
      top: 28px;
      left: 23px;
      width: 2px;
      height: calc(100% - 28px);
      background: #c3c3c3;
    }
  }
}
</style>
<style lang="less">
.ry-video-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
</style>
